<?php
/**
 * 路由处理类
 */

namespace Hi;
class Router {

    /**
     * Request实例
     * @var
     */
    private $_request;

    /**
     * 控制器
     * @var
     */
    public $_controller;

    /**
     * 动作
     * @var
     */
    public $_action;

    /**
     * 路由方式
     * @var
     */
    protected $_routeType;

    /**
     * 路由数组
     * @var
     */
    protected $_uriArray;

    /**
     * 命令行参数数组
     * @var array
     */
    protected $_cliParamsArray = array();

    public function __construct() {
        $this->_request = Request::getInstance();

        if (true === $this->isCli()) {

        } elseif  (false === strpos($_SERVER['REQUEST_URI'],$_SERVER['SCRIPT_NAME'])) {
            //Rewrite (/Controller/Action/param1/value1/param2/value2
            $this->_routeType = 'rewrite';
            $this->_uriArray = $this->parseUrlToArray();

            $controller = (isset($this->_uriArray[1]) && !empty($this->_uriArray[1])) ? $this->_uriArray[1] : 'index';
            $action = (isset($this->_uriArray[2]) && !empty($this->_uriArray[2])) ? $this->_uriArray[2] : 'index';
        } else {
            // GET (/index.php?c=index&a=index)

            $this->_routeType = 'get';
            if (empty($_SERVER['QUERY_STRING'])) {
                $controller = $action = 'index';
            } else {
                $queryStringArray = $this->_request->getQueryStringArray();
                $controller = isset($queryStringArray['c']) ? $queryStringArray['c'] : 'index';
                $action = isset($queryStringArray['a']) ? $queryStringArray['a'] : 'index';
            }
        }

        if ($this->checkRoute($controller)){
            $this->_request->setControllerName($controller);
        } else {
            throw new Exceptions('Controller name invalid.', 404);
        }

        if ($this->checkRoute($action)) {
            $this->_request->setActionName(strtolower($action));
        } else {
            throw new Exceptions('Action name invalid.', 404);
        }
    }

    /**
     * 读取路由方式
     * @return mixed
     */
    public function getRouteType() {
        return $this->_routeType;
    }

    private function parseUrlToArray() {
        $requestUri = '';
        if(empty($_SERVER['QUERY_STRING'])) {
            $requestUri = $_SERVER['REQUEST_URI'];
        } else {
            $requestUri = str_replace('?' . $_SERVER['QUERY_STRING'], '', $_SERVER['REQUEST_URI']);
        }
        $baseUrl = $this->_request->getBaseUrl();

        if ($requestUri != $baseUrl) {
            $requestUri = str_replace($baseUrl, '', $requestUri);
        }
        $uriArray = explode('/', $requestUri);

        return $uriArray;
    }

    /**
     * 检查路由参数合法性
     * @param $value
     * @return int
     */
    protected function checkRoute($value) {
        return preg_match("/^[a-zA-Z][a-zA-Z0-9]*$/",$value);
    }

    public function isCli() {

    }
}